const bannersModel = require('../model/bannersModel');

//新增banner
module.exports.insert = async function (banner) {
    return await bannersModel.create(banner);
}

//删除banner
module.exports.delete = async function (id) {
    return await bannersModel.deleteOne({ _id: id });
}

//查询banner
module.exports.find = async function (page, searchParams) {
    let { current, pageSize } = page;
    // 根据查询条件获取总数
    let total = await bannersModel.countDocuments(searchParams);
    // 根据查询条件获取具体数量的数据
    let data = await bannersModel.find(searchParams)
        .limit(pageSize)//限制查询数量
        .skip((current - 1) * pageSize);//从下标0-n开始查询
    return { current, pageSize, total, rows: data }//rows:data每行显示数据
}

//修改banner
module.exports.update = async function (banner) {
    return await bannersModel.updateOne({ _id: banner._id }, banner);
}

//通过ID查询
module.exports.findById = async function(id){
    return await bannersModel.findById(id);
}